import { Module } from 'vuex'
import { ISystemState } from './types'
import { IRootState } from '../types'
import { getPageListData, deletePageData, createPageData, editPageData } from '@/service/main/system/system'

const systemModule: Module<ISystemState, IRootState> = {
  namespaced: true,
  state() {
    return {
      queryInfo: {},
      usersList: [],
      usersCount: 0,
      roleList: [],
      roleCount: 0,
      goodsList: [],
      goodsCount: 0,
      menuList: [],
      menuCount: 0,
    }
  },
  mutations: {
    saveQueryInfo(state, queryInfo: any) {
      state.queryInfo = queryInfo
    },
    saveUsersList(state, usersList: any) {
      state.usersList = usersList
    },
    saveUsersCount(state, usersCount: any) {
      state.usersCount = usersCount
    },
    saveRoleList(state, roleList: any) {
      state.roleList = roleList
    },
    saveRoleCount(state, roleCount: any) {
      state.roleCount = roleCount
    },
    saveGoodsList(state, goodsList: any) {
      state.goodsList = goodsList
    },
    saveGoodsCount(state, goodsCount: any) {
      state.goodsCount = goodsCount
    },
    saveMenuList(state, menuList: any) {
      state.menuList = menuList
    },
    saveMenuCount(state, menuCount: any) {
      state.menuCount = menuCount
    }
  },
  getters: {
    pageListData(state: any) {
      return (pageName: string) => {
        return state[`${pageName}List`]
      }
    },
    pageListCount(state: any) {
      return (pageName: string) => {
        return state[`${pageName}Count`]
      }
    }
  },
  actions: {
    async getPageListAction({ commit }, payload: any) {
      // 1. 请求用户列表并保存
      const pageName = payload.pageName
      let pageUrl = ''
      switch (pageName) {
        case 'users':
          pageUrl = '/users/list'
          break
        case 'role':
          pageUrl = '/role/list'
          break
        case 'goods':
          pageUrl = '/goods/list'
          break
        case 'menu':
          pageUrl = '/menu/list'
          break
      }
      const pageListResult = await getPageListData(pageUrl, payload.queryInfo)
      const { list, totalCount } = pageListResult.data
      const saveName = pageName.charAt(0).toUpperCase() + pageName.slice(1)
      commit(`save${saveName}List`, list)
      commit(`save${saveName}Count`, totalCount)
    },

    async deletePageDataAction({ dispatch, state }, payload: any) {
      const { pageName, id } = payload
      const pageUrl = `/${pageName}/${id}`
      // 删除数据
      await deletePageData(pageUrl)
      // 重新请求更新数据
      await dispatch('getPageListAction', {
        pageName,
        queryInfo: state.queryInfo
      })
    },

    async createPageDataAction({ dispatch, state }, payload: any) {
      const { pageName, newData } = payload
      const pageUrl = `${pageName}`
      // 创建数据
      await createPageData(pageUrl, newData)
      // 重新请求更新数据
      await dispatch('getPageListAction', {
        pageName,
        queryInfo: state.queryInfo
      })
    },

    async editPageDataAction({ dispatch, state }, payload: any) {
      const { pageName, editData,id } = payload
      const pageUrl = `${pageName}/${id}`
      // 修改数据
      await editPageData(pageUrl, editData)
      // 重新请求更新数据
      await dispatch('getPageListAction', {
        pageName,
        queryInfo: state.queryInfo
      })
    },
  }
}

export default systemModule
